package com.java.business.controller.admin;

import java.util.Arrays;
import java.util.Map;
import javax.validation.Valid;

import cn.hutool.core.util.StrUtil;
import com.java.business.service.CourseService;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.java.business.entity.Section;
import com.java.business.service.SectionService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;


/**
 * 小节
 *
 * @author jl
 * @since 2020-10-07 10:31:35
 */
@RestController
@RequestMapping("admin/section")
public class SectionController {
    @Autowired
    private SectionService sectionService;

    /**
     * 带条件分页查询小节
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sectionService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 根据主键查询小节
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        Section section = sectionService.getById(id);

        return R.ok(section);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody Section section) {
        if (StrUtil.isNotBlank(section.getId())) {
            sectionService.updateById(section);
        } else {
            section.setId(UuidUtil.getShortUuid());
            sectionService.save(section);
        }
        return R.ok(section);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        sectionService.removeById(id);
        return R.ok();
    }

}
